<template>
  <div>
    <a-input :disabled="disabled" v-model="textVals" :class="textVals?'has-value':''" placeholder="请选择" readOnly unselectable="on" @click="onSearchDepUser" @search="onSearchDepUser" v-if="showInput">
      <a-icon slot="suffix" type="search" @click="onSearchDepUser" class="down-icon" />
      <a-icon slot="suffix" type="close-circle" theme="filled" class="close-icon" @click="selectOK([])" />
      <!-- <a-button slot="enterButton" :disabled="disabled">选择用户</a-button> -->
    </a-input>
    <j-select-user-by-dep-jke-modal ref="selectModal" :modal-width="modalWidth" :multi="multi" :limit="limit" @ok="selectOK" @close="close" :user-ids="value" :store="storeField" :text="textField" @initComp="initComp" />
  </div>
</template>

<script>
import JSelectUserByDepJkeModal from './modal/JSelectUserByDepJkeModal'
import { underLinetoHump } from '@/components/_util/StringUtil'

export default {
  name: 'JSelectUserByDepJke',
  components: { JSelectUserByDepJkeModal },
  props: {
    limit: {
      type: Number,
      default: 0,
      required: false,
    },
    modalWidth: {
      type: Number,
      default: 1250,
      required: false,
    },
    value: {
      type: String,
      required: false,
    },
    disabled: {
      type: Boolean,
      required: false,
      default: false,
    },
    multi: {
      type: Boolean,
      default: true,
      required: false,
    },
    backUser: {
      type: Boolean,
      default: false,
      required: false,
    },
    showInput: {
      type: Boolean,
      default: true,
      required: false,
    },
    userIndex: {
      type: Number,
      default: 0,
      required: false,
    },
    // 存储字段 [key field]
    store: {
      type: String,
      default: 'id',
      required: false,
    },
    // 显示字段 [label field]
    text: {
      type: String,
      default: 'realname',
      required: false,
    },
  },
  data() {
    return {
      storeVals: '', //[key values]
      textVals: '', //[label values]
    }
  },
  computed: {
    storeField() {
      let field = this.customReturnField
      if (!field) {
        field = this.store
      }
      return underLinetoHump(field)
    },
    textField() {
      return underLinetoHump(this.text)
    },
  },
  mounted() {
    this.storeVals = this.value
  },
  watch: {
    value(val) {
      this.storeVals = val
    },
  },
  model: {
    prop: 'value',
    event: 'change',
  },
  methods: {
    close() {
      this.$emit('close')
    },
    initComp(textVals) {
      this.textVals = textVals
    },
    clearSelect() {
      this.$emit('allowClear')
    },
    //返回选中的用户信息
    backDeparInfo() {
      if (this.backUser === true) {
        if (this.storeVals && this.storeVals.length > 0) {
          let arr1 = this.storeVals.split(',')
          let arr2 = this.textVals.split(',')
          let info = []
          for (let i = 0; i < arr1.length; i++) {
            info.push({
              value: arr1[i],
              text: arr2[i],
            })
          }
          this.$emit('back', info)
        }
      }
    },
    onSearchDepUser() {
      this.$refs.selectModal.showModal()
    },
    selectOK(rows) {
      console.log('当前选中用户', rows)
      if (!rows) {
        this.storeVals = ''
        this.textVals = ''
      } else {
        let temp1 = []
        let temp2 = []
        let temp3 = []
        for (let item of rows) {
          temp1.push(item[this.storeField])
          temp2.push(item[this.textField])
          temp3.push(item.id)
        }
        this.storeVals = temp1.join(',')
        this.textVals = temp2.join(',')
      }
      this.$emit('change', this.storeVals)
      this.$emit('changeRow', this.storeVals, rows, this.userIndex)
    },
  },
}
</script>

<style lang="less" scoped>
.has-value:hover {
  .down-icon {
    display: none;
  }
  .close-icon {
    display: block;
  }
}
.close-icon {
  display: none;
  color:rgba(0, 0, 0, 0.25);
  font-size:14px;
}
.close-icon :hover {
  color:rgba(0, 0, 0, 0.5);
}
</style>